﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcmyjobs.Web.enprise
{
    public partial class ListReceive : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Application["webname"] != null)
                {
                    this.Title = string.Format("单位管理中心—简历管理：应聘您职位的简历  {0}", Application["webname"].ToString());
                }
                int enterprise_bid = -1;

                if (Session["enterprise_bid"] != null)
                {
                    enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                    Bind(enterprise_bid);

                }
                else
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请先登陆！", "EnterpriseLogin.html");
                }

            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            int enterprise_bid = -1;
            if (Session["enterprise_bid"] != null)
            {
                enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                Bind(enterprise_bid);

            }
        }

        private void Bind(int enterprise_bid)
        {
            int count = 0;

            DataSet ds = new lcmyjobs.BLL.enterprise_receive().GetReceive(enterprise_bid, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out count);

            if (ds.Tables[1].Rows.Count > 0)
            {
                this.lbl_title.Text = " " + ds.Tables[1].Rows[0]["name"].ToString();
                //ds.Tables[1].Rows[0]["name"] = ds.Tables[1].Rows[0]["name"].ToString() + "&bull;" + ds.Tables[1].Rows[0]["sexname"].ToString() + "&bull;" + ds.Tables[1].Rows[0]["edu_name"].ToString();
                this.lbl_count.Text = count.ToString();
            }

            this.AspNetPager1.RecordCount = count;

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                if (e.CommandArgument != null)
                {
                    int enterprise_receive_id = Convert.ToInt32(e.CommandArgument);

                    lcmyjobs.BLL.enterprise_receive er = new BLL.enterprise_receive();

                    if (er.Delete(enterprise_receive_id))
                    {
                        int enterprise_bid = -1;

                        if (Session["enterprise_bid"] != null)
                        {
                            enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                            Bind(enterprise_bid);

                        }
                    }

                }

            }
            if (e.CommandName == "look")
            {
                if (e.CommandArgument != null)
                {
                    int enterprise_receive_id = Convert.ToInt32(e.CommandArgument);
                    lcmyjobs.BLL.enterprise_receive er = new BLL.enterprise_receive();
                    lcmyjobs.BLL.enterprise_read read = new BLL.enterprise_read();
                    lcmyjobs.Model.enterprise_receive em = new Model.enterprise_receive();
                    em.enterprise_receive_id = enterprise_receive_id;
                    GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                    Label lb = row.FindControl("lbl_uid") as Label;
                    int uid = 0;
                    int bid = 0;
                    if (!string.IsNullOrEmpty(lb.Text))
                    {
                        uid = Convert.ToInt32(lb.Text);
                    }
                    if (Session["enterprise_bid"] != null)
                    {
                        bid = Convert.ToInt32(Session["enterprise_bid"]);
                    }
                    if (er.UpdateIsbrowse(enterprise_receive_id))//取消了会员权限的验证，改为统一在users/show_user.aspx页面验证，因为一验证就相当去购买了
                    {

                        //string str = "window.open('../users/show_user.aspx?id=" + lb.Text + "')";

                        //Maticsoft.Common.MessageBox.ResponseScript(this, str);

                        //string str = HttpContext.Current.Request.Url.Host + "/users/show_user.aspx?id=" + lb.Text ;
                        //this.GridView1.Target = "_blank";
                        Bind(bid);
                        string str = "../users/show_user-" + lb.Text + ".html";
                        //Response.Redirect( str );

                        string strp = "<script languge='javascript'>window.open('"+str+"')</script>";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "a", strp);
                        
                    }

                    //int ispower = read.GetMemberPower(bid, uid);//验证会员权限

                    //if (ispower == 1)//如果会员可读此简历
                    //{
                    //    if (er.UpdateIsbrowse(enterprise_receive_id))
                    //    {
                    //        //Response.Redirect("../users/show_user.aspx?id=" + lb.Text);

                    //        string str = "window.open('../users/show_user.aspx?id="+lb.Text+"')";

                    //        Maticsoft.Common.MessageBox.ResponseScript(this, str);
                    //        Bind(bid);
                    //    }
                    //}
                    //else
                    //{
                    //    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "您没有权限读此简历，请升级会员权限！", "ListMember.aspx");
                    //}

                }
            }
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.GridView1.Rows[i].FindControl("cbo_rid");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (GridView1.DataKeys[i].Value != null)
                    {
                        idlist += GridView1.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        //批量删除
        protected void lbn_delete_Click(object sender, EventArgs e)
        {
            lcmyjobs.BLL.enterprise_receive bll = new BLL.enterprise_receive();
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length != 0)
            {
                if (bll.DeleteList(idlist))
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "删除成功！", "ListReceive.html");
                }
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请选择所要删除的简历！", "ListReceive.html");
            }


        }

    }
}